<?php
ini_set('display_errors','on');
error_reporting(E_ALL);

require_once 'sample_base.php';
//初始化
$bucket = SampleUtil::get_bucket_name();
$oss = SampleUtil::get_oss_client();

/*%**************************************************************************************************************%*/
//bucket 相关示例
/**
 *删除bucket
 *如果bucket不为空则bucket无法删除成功
 *不为空表示bucket既没有object，也没有未完成的multipart上传时的parts
 */
//$res = $oss->delete_bucket($bucket);
//car_base/  left_front45

$options = array(
   'delimiter' => '/',
   'prefix' => 'car_base/left_front45/',
   //'prefix' => 'damage/',
   'max-keys' => 100,
   'marker' => '',
);
//批量删除文件/文件夹
$res = $oss->list_object($bucket,$options);
if ($res->isOk()){
    $body = $res->body;
    $xml = new SimpleXMLElement($body);
    foreach ($xml->Contents as $content) {
        //$oss->delete_object($bucket,$content->Key);
        echo $content->Key.'<br>';
    }
    foreach ($xml->CommonPrefixes as $content) {
        if($content->Prefix=='car_base/')continue;
        if($content->Prefix=='damage/')continue;

        SampleUtil::my_echo("子目录列表: " . $content->Prefix ."<br/>");
        //$oss->delete_object($bucket,$content->Prefix);
    }
}
exit;
$base_pic_pos = array(
                            'left_front45' => '左前45度',
                            'right_after45' => '右后45度',
                            'appearance_front_row' => '前排',//内饰-前排
                            'appearance_back_row' => '后排',//内饰-后排
                            'appearance_control' => '中控台',//内饰-中控
                            'instrument_panel' => '仪表盘',
                            'engine_room_l' => '发动机舱左',
                            'engine_room_r' => '发动机舱右',
                            'engine_room' => '发动机舱',
                            'trunk' => '后备箱',
                            'key' => '车钥匙',
                            'car_top' => '顶棚',
                            'vin' => '车架号',
                            'travel_license' => '行驶本',
                            'nameplate' => '铭牌号'
                );
$base_pic_pos = array_keys($base_pic_pos);
$options = null;
//foreach($base_pic_pos as $dir_name){
//  //  $oss->create_object_dir($bucket,'car_base/'.$dir_name,$options);
//}
$visible_damage_pos = array(
                            'l_f_door'=>'左前门内饰',
                            'r_f_door'=>'右前门内饰',
                            'l_b_door'=>'左后门内饰',
                            'r_b_door'=>'右后门内饰',
                            'glove_box ' => '手套箱',
                            'car_roof' => '车棚顶',
                            'floor_gluten' => '车内地胶',
                            'gear_handle' => '排挡把手柄',
                            'armrest_box' => '扶手箱',
                            'steering_wheel' => '方向盘',
                            'l_f_door_armrest' => '左前门扶手',
                            'r_f_door_armrest' => '右前门扶手',
                            'l_b_door_armrest' => '左后门扶手',
                            'r_b_door_armrest' => '右后门扶手',
                            'appearance_control' => '中控台',
                            'main_drive_seat' => '主驾座椅',
                            'copilot_seat' => '副驾座椅',
                            'b_row_seat' => '后排座椅',
                            'instrument_panel' => '仪表',
                            'trunk' => '后备箱',
                            'l_f_wing_board' => '左前翼子板',
                            'r_f_wing_board' => '右前翼子板',
                            'engine_cover' => '引擎盖',
                            'l_b_wing_board' => '左后翼子板',
                            'r_b_wing_board' => '右后翼子板',
                            'trunk_cover' => '行李箱盖',
                            'car_top' => '车顶',
                            'f_insure_bar' => '前保险杠',
                            'b_insure_bar' => '后保险杠',
                            'air_inlet_grille ' => '进气格栅',
                            'aerial' => '天线',
                            'l_f_comb_light' => '左前组合灯',
                            'r_f_comb_light' => '右前组合灯',
                            'r_f_fog_light' => '右前雾灯',
                            'l_f_fog_light' => '左前雾灯',
                            'r_side_light' => '右侧边灯',
                            'l_side_light' => '左侧边灯',
                            'r_b_comb_light' => '右后组合灯',
                            'l_b_comb_light' => '左后组合灯',
                            'l_f_tire' => '左前轮胎',
                            'l_b_tire' => '左后轮胎',
                            'r_f_tire' => '右前轮胎',
                            'r_b_tire' => '右后轮胎',
                            'bak_tire' => '备胎',
                            'l_f_tire_hub' => '左前轮毂',
                            'l_b_tire_hub' => '左后轮毂',
                            'r_f_tire_hub' => '右前轮毂',
                            'r_b_tire_hub' => '右后轮毂',
                            'l_mirror' => '左反光镜',
                            'r_mirror' => '右反光镜',
                            'l_f_door_glass' => '左前门玻璃',
                            'l_b_door_glass' => '左后门玻璃',
                            'r_f_door_glass' => '右前门玻璃',
                            'f_windshield' => '前挡风玻璃',
                            'b_windshield' => '后挡风玻璃',
                            'l_triangle_glass' => '左侧三角玻璃',
                            'r_triangle_glass' => '右侧三角玻璃',
                            'l_f_doorknob_lock' => '左前门拉手（锁)',
                            'l_b_doorknob_lock' => '左后门拉手（锁)',
                            'r_f_doorknob_lock' => '右前门拉手（锁)',
                            'r_b_doorknob_lock' => '右后门拉手（锁)',
                            'trunk_lock' => '后备箱（锁)',
                            'engine_cover_lock' => '引擎盖（锁)',
                            'radiator_mounting' => '水箱支架',
                            'l_f_light_frame' => '左前照灯框架',
                            'r_f_light_frame' => '右前照灯框架',
                            'l_f_wing_board_lining' => '左前翼子板内衬',
                            'r_f_wing_board_lining' => '右前翼子板内衬',
                            'r_b_light_frame' => '右后尾灯框架',
                            'l_b_light_frame' => '左后尾灯框架',
                            'b_surround_board' => '后围板',
                            'l_a_bar' => '左A柱',
                            'l_b_bar' => '左B柱',
                            'l_c_bar' => '左C柱',
                            'r_c_bar' => '右C柱',
                            'r_b_bar' => '右B柱',
                            'r_a_bar' => '右A柱',
                            'l_f_vertical_beam' => '左前纵梁',
                            'r_f_vertical_beam' => '右前纵梁',
                            'l_f_absorber_seat' => '左前减震器座',
                            'r_f_absorber_seat' => '右前减震器座',
                            'firewall' => '防火墙',
                            'l_b_vertical_beam' => '左后纵梁',
                            'r_b_vertical_beam' => '右后纵梁',
                            'l_b_absorber_seat' => '左后减震器座',
                            'r_b_absorber_seat' => '右后减震器座',
                            'trunk_bot_board' => '行李箱底板'
                        );
$visible_damage_pos = array_keys($visible_damage_pos);
$options = null;
foreach($visible_damage_pos as $dir_name){
        //$oss->create_object_dir($bucket,'damage/'.$dir_name,$options);
}


exit;
//$options = array();
//
//$objects = array(
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688569.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG',
//            'car_base/left_front45/16321265409886_3030856_16_1445688367.JPG'
//        );
//
//$options = array(
//    'quiet' => false,
//);
//
//$res = $oss->delete_objects($bucket,$objects,$options);
exit;
//$msg = "删除bucket " . $bucket;
//OSSUtil::print_res($res, $msg);

/**
 *创建bucket
 *acl 指的是bucket的访问控制权限，有三种，私有读写，公共读私有写，公共读写。私有读写就是只有bucket的owner才有权限操作
 *三种权限分别对应ALIOSS::OSS_ACL_TYPE_PRIVATE，ALIOSS::OSS_ACL_TYPE_PUBLIC_READ, ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE 
 */
//$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
//$res = $oss->create_bucket($bucket, $acl);
//$msg = "创建bucket " . $bucket;
//OSSUtil::print_res($res, $msg);

/**
 *设置bucket acl
 */
//$acl = ALIOSS::OSS_ACL_TYPE_PRIVATE;
//$res = $oss->set_bucket_acl($bucket, $acl);
//$msg = "设置 bucket " . $bucket . " acl 为 " . $acl;
//OSSUtil::print_res($res, $msg);

/**
 *获取bucket acl
 */
//$res = $oss->get_bucket_acl($bucket);
//$msg = "获取 bucket " . $bucket . " acl";
//OSSUtil::print_res($res, $msg);
//if ($res->isOK()) {
//    $xml = new SimpleXMLElement($res->body);
//    SampleUtil::my_echo("Bucket acl is " . $xml->AccessControlList->Grant);
//}

/**
 *显示创建的bucket
 *列出用户所有的Bucket
 */
//$res = $oss->list_bucket();
//$msg = "<p>列出用户所有的Bucket</p>";
//OSSUtil::print_res($res, $msg);
//echo '<br/>';
//$index = 1;
//$max = 10;
//if ($res->isOk()){
//    $xml = new SimpleXMLElement($res->body);
//    $_bucket_list = array();
//    foreach ( $xml->Buckets->Bucket as $_buckets) {
//        array_push($_bucket_list, $_buckets->Name);
//    }
//    foreach ($_bucket_list as $key) {
//        SampleUtil::my_echo("Bucket No. " . $index . ": " . $key.'<br/>');
//        $index++;
//        if ($index > $max) {
//            SampleUtil::my_echo("for test, list " . $max . " buckets at most, break<br/>");
//            break;
//        }
//    }
//}
//
//echo '<br><br>';
/**
 *列出目录下的文件和子目录
 */
$prefix = '';
$marker = '';
$delimiter = '/';
$next_marker = '';
$maxkeys = 1000;
//$index = 1;
$options = array(
        'delimiter' => $delimiter,
        'prefix' => $prefix,
        'max-keys' => $maxkeys,
        'marker' => $next_marker,
    );
//
//$res = $oss->list_object($bucket, $options);
//echo '<pre>';print_r($res);echo '</pre>';
//exit;
//$msg = "列出目录下的文件和子目录: " . $bucket ."<br>";
//OSSUtil::print_res($res, $msg);
//if ($res->isOk()){
//    $body = $res->body;
//    echo '<hr><pre>';
//    print_r($body);
//    echo '</pre><hr>';
//    $xml = new SimpleXMLElement($body);
//    echo '<pre>';
//    print_r($xml);
//    echo '</pre>';
//    foreach ($xml->Contents as $content) {
//        SampleUtil::my_echo("文件列表: " . $content->Key." <br/>");
//    }
//    foreach ($xml->CommonPrefixes as $content) {
//        SampleUtil::my_echo("子目录列表: " . $content->Prefix ."<br/>");
//    }
//}
/**
 *创建模拟文件夹
 *OSS服务是没有文件夹这个概念的，所有元素都是以Object来存储。但给用户提供了创建模拟文件夹的方式
 */
//$object = 'car_base/left_front45/test/';
//$res = $oss->create_object_dir($bucket, $object);
//$res = $oss->delete_object($bucket, $object);
//$msg = "创建模拟文件夹 /" . $bucket . "/" . $object;

$options = array();

$file_path = '16321265409886_3030856_16.JPG';
$object = 'car_base/left_front45/'.create_uuid('left_front45_').'.JPG';
$content = file_get_contents($file_path);
$options = array(
            'content' => $content,
            'length' => strlen($content),

            ALIOSS::OSS_HEADERS => array(
                'Expires' => 'Fri, 28 Feb 2012 05:38:42 GMT',
                'Cache-Control' => 'no-cache',
                'Content-Disposition' => 'attachment;filename=oss_download.log',
                'Content-Encoding' => 'utf-8',
                'Content-Language' => 'zh-CN',
                'x-oss-server-side-encryption' => 'AES256',
            ),
        );

try{
    //$res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
    $res = $oss->upload_file_by_content($bucket,$object,$options);
}catch(Exception $e){
    echo '<pre>';
    print_r($e->getMessage());
    echo '</pre>';
    echo '<pre>';
    print_r($e->getCode());
    echo '</pre>';
    echo '<pre>';
    print_r($e->getFile());
    echo '</pre>';
    echo '<pre>';
    print_r($e->getTrace());
    echo '</pre>';
    echo '<pre>';
    //print_r($e->getLine());
    echo '</pre>';
    $res = '';
}
//$object = rtrim($object,'.JPG');
$src = 'http://'.$bucket.'.oss-cn-beijing.aliyuncs.com/'.$object;
echo $src;
echo "<img src='".$src."' />";
echo '<pre>';
print_r($res);
echo '</pre>';
//$res = $oss->is_object_exist($bucket, $object);
//$res = $oss->delete_object($bucket, $object);


function _upfile_oss($key,$value){
    if(!$value) return "";
    if(!$key){
        $key = create_uuid('**').".jpg";//这里的key我通过自己创建的UUID外加开头和结尾组成
										//如果希望在某个目录下，这个开头可以写成-----dir-name/-----都懂得，分目录么
    }
    global $oss;
    global $bucket;
    $oss->putObject(array(
        'Bucket' => $bucket,
        'Key' => $key,
        'Content' => $value,
        'ContentLength' => strlen($value),
    ));
    return "http://{$bucket}.oss-cn-qingdao.aliyuncs.com/{$key}";
}
function create_uuid($prefix = ""){    //可以指定前缀
    $str = md5(uniqid(mt_rand(), true));
    $uuid  = substr($str,0,8) . '-';
    $uuid .= substr($str,8,4) . '-';
    $uuid .= substr($str,12,4) . '-';
    $uuid .= substr($str,16,4) . '-';
    $uuid .= substr($str,20,12);
    return $prefix . $uuid;
}
?>